<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>任务管理系统 - 分类管理</title>
    <!-- 引入Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- 引入Font Awesome -->
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <!-- 引入自定义样式 -->
    <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
</head>
<body>
    <!-- 导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
        <div class="container-fluid">
            <a class="navbar-brand" href="{{ url_for('dashboard') }}">
                <i class="fa fa-tasks mr-2"></i>任务管理系统
            </a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="切换导航">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('dashboard') }}">
                            <i class="fa fa-dashboard mr-1"></i>仪表盘
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('task_list') }}">
                            <i class="fa fa-list mr-1"></i>任务列表
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link active" aria-current="page" href="{{ url_for('category_list') }}">
                            <i class="fa fa-folder mr-1"></i>分类管理
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('reports') }}">
                            <i class="fa fa-bar-chart mr-1"></i>数据报表
                        </a>
                    </li>
                </ul>
                <div class="navbar-nav">
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                            <i class="fa fa-user-circle mr-1"></i>{{ session.username }}
                        </a>
                        <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdown">
                            <li>
                                <a class="dropdown-item" href="{{ url_for('notifications') }}">
                                    <i class="fa fa-bell mr-1"></i>通知
                                </a>
                            </li>
                            <li>
                                <a class="dropdown-item" href="{{ url_for('settings') }}">
                                    <i class="fa fa-cog mr-1"></i>设置
                                </a>
                            </li>
                            <li><hr class="dropdown-divider"></li>
                            <li>
                                <a class="dropdown-item" href="{{ url_for('logout') }}">
                                    <i class="fa fa-sign-out mr-1"></i>退出登录
                                </a>
                            </li>
                        </ul>
                    </li>
                </div>
            </div>
        </div>
    </nav>

    <!-- 主要内容区域 -->
    <div class="container-fluid mt-4">
        <!-- 消息提示区域 -->
        {% with messages = get_flashed_messages(with_categories=true) %}
            {% if messages %}
                {% for category, message in messages %}
                    <div class="alert alert-{{ category }} alert-dismissible fade show" role="alert">
                        {{ message }}
                        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="关闭"></button>
                    </div>
                {% endfor %}
            {% endif %}
        {% endwith %}

        <!-- 页面标题和操作按钮 -->
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2>
                <i class="fa fa-folder mr-2"></i>分类管理
            </h2>
            <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addCategoryModal">
                <i class="fa fa-plus-circle mr-1"></i>新建分类
            </button>
        </div>

        <!-- 分类列表 -->
        <div class="card">
            <div class="card-header bg-white border-bottom border-gray-200">
                <h4 class="card-title mb-0">分类列表</h4>
            </div>
            <div class="card-body p-0">
                {% if categories %}
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead class="bg-light">
                                <tr>
                                    <th scope="col">名称</th>
                                    <th scope="col">颜色</th>
                                    <th scope="col">任务数量</th>
                                    <th scope="col">创建时间</th>
                                    <th scope="col">操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for category in categories %}
                                    <tr>
                                        <td>
                                            <div class="d-flex align-items-center gap-2">
                                                <div class="w-3 h-3 rounded-full" style="background-color: {{ category.color }}"></div>
                                                {{ category.name }}
                                            </div>
                                        </td>
                                        <td>
                                            <div class="d-flex align-items-center gap-2">
                                                <div class="w-6 h-6 rounded-full" style="background-color: {{ category.color }}"></div>
                                                <span class="text-muted">{{ category.color }}</span>
                                            </div>
                                        </td>
                                        <td>{{ category.task_count }}</td>
                                        <td>{{ category.created_at.strftime('%Y-%m-%d') }}</td>
                                        <td>
                                            <div class="btn-group" role="group">
                                                <button type="button" class="btn btn-sm btn-primary edit-category" data-category-id="{{ category.id }}" data-category-name="{{ category.name }}" data-category-color="{{ category.color }}">
                                                    <i class="fa fa-pencil"></i>
                                                </button>
                                                <button type="button" class="btn btn-sm btn-danger delete-category" data-category-id="{{ category.id }}" data-category-name="{{ category.name }}" {% if category.task_count > 0 %}disabled{% endif %}>
                                                    <i class="fa fa-trash"></i>
                                                </button>
                                            </div>
                                        </td>
                                    </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                {% else %}
                    <div class="text-center text-muted py-8">
                        <i class="fa fa-folder-open-o fa-4x mb-3"></i>
                        <p class="h5">暂无分类</p>
                        <p>点击上方按钮创建第一个分类</p>
                    </div>
                {% endif %}
            </div>
        </div>
    </div>

    <!-- 添加分类模态框 -->
    <div class="modal fade" id="addCategoryModal" tabindex="-1" aria-labelledby="addCategoryModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="addCategoryModalLabel">新建分类</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="关闭"></button>
                </div>
                <form method="POST" action="{{ url_for('add_category') }}" id="addCategoryForm">
                    <div class="modal-body">
                        <div class="mb-3">
                            <label for="category_name" class="form-label">分类名称 <span class="text-danger">*</span></label>
                            <input type="text" class="form-control" id="category_name" name="name" required placeholder="请输入分类名称">
                        </div>
                        <div class="mb-3">
                            <label for="category_color" class="form-label">分类颜色 <span class="text-danger">*</span></label>
                            <div class="d-flex gap-2">
                                <input type="color" class="form-control form-control-color" id="category_color" name="color" value="#4F46E5" required title="选择颜色">
                                <input type="text" class="form-control" id="category_color_text" name="color_text" value="#4F46E5" required placeholder="#4F46E5">
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                        <button type="submit" class="btn btn-primary">保存</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- 编辑分类模态框 -->
    <div class="modal fade" id="editCategoryModal" tabindex="-1" aria-labelledby="editCategoryModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="editCategoryModalLabel">编辑分类</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="关闭"></button>
                </div>
                <form method="POST" action="" id="editCategoryForm">
                    <input type="hidden" name="category_id" id="edit_category_id">
                    <div class="modal-body">
                        <div class="mb-3">
                            <label for="edit_category_name" class="form-label">分类名称 <span class="text-danger">*</span></label>
                            <input type="text" class="form-control" id="edit_category_name" name="name" required placeholder="请输入分类名称">
                        </div>
                        <div class="mb-3">
                            <label for="edit_category_color" class="form-label">分类颜色 <span class="text-danger">*</span></label>
                            <div class="d-flex gap-2">
                                <input type="color" class="form-control form-control-color" id="edit_category_color" name="color" required title="选择颜色">
                                <input type="text" class="form-control" id="edit_category_color_text" name="color_text" required placeholder="#4F46E5">
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                        <button type="submit" class="btn btn-primary">更新</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- 删除确认模态框 -->
    <div class="modal fade" id="deleteCategoryModal" tabindex="-1" aria-labelledby="deleteCategoryModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header bg-danger text-white">
                    <h5 class="modal-title" id="deleteCategoryModalLabel">确认删除</h5>
                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="关闭"></button>
                </div>
                <div class="modal-body">
                    <p>确定要删除分类 <span id="delete_category_name" class="font-weight-bold"></span> 吗？</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <form id="deleteCategoryForm" method="POST" style="display: inline;">
                        <button type="submit" class="btn btn-danger">确认删除</button>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <!-- 引入Bootstrap JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
    <!-- 引入自定义JS -->
    <script src="{{ url_for('static', filename='js/main.js') }}"></script>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 颜色输入框同步
            const colorInput = document.getElementById('category_color');
            const colorTextInput = document.getElementById('category_color_text');
            
            colorInput.addEventListener('input', function() {
                colorTextInput.value = this.value;
            });
            
            colorTextInput.addEventListener('input', function() {
                // 简单验证是否为有效的颜色值
                if (this.value.match(/^#[0-9A-F]{6}$/i)) {
                    colorInput.value = this.value;
                }
            });
            
            // 编辑分类模态框
            const editCategoryModal = new bootstrap.Modal(document.getElementById('editCategoryModal'));
            const editCategoryForm = document.getElementById('editCategoryForm');
            const editColorInput = document.getElementById('edit_category_color');
            const editColorTextInput = document.getElementById('edit_category_color_text');
            const editCategoryIdInput = document.getElementById('edit_category_id');
            const editCategoryNameInput = document.getElementById('edit_category_name');
            
            // 颜色输入框同步
            editColorInput.addEventListener('input', function() {
                editColorTextInput.value = this.value;
            });
            
            editColorTextInput.addEventListener('input', function() {
                if (this.value.match(/^#[0-9A-F]{6}$/i)) {
                    editColorInput.value = this.value;
                }
            });
            
            // 点击编辑按钮
            document.querySelectorAll('.edit-category').forEach(button => {
                button.addEventListener('click', function() {
                    const categoryId = this.getAttribute('data-category-id');
                    const categoryName = this.getAttribute('data-category-name');
                    const categoryColor = this.getAttribute('data-category-color');
                    
                    editCategoryIdInput.value = categoryId;
                    editCategoryNameInput.value = categoryName;
                    editColorInput.value = categoryColor;
                    editColorTextInput.value = categoryColor;
                    
                    editCategoryForm.action = `/categories/${categoryId}/edit`;
                    editCategoryModal.show();
                });
            });
            
            // 删除分类模态框
            const deleteCategoryModal = new bootstrap.Modal(document.getElementById('deleteCategoryModal'));
            const deleteCategoryForm = document.getElementById('deleteCategoryForm');
            const deleteCategoryNameElement = document.getElementById('delete_category_name');
            
            // 点击删除按钮
            document.querySelectorAll('.delete-category').forEach(button => {
                button.addEventListener('click', function() {
                    const categoryId = this.getAttribute('data-category-id');
                    const categoryName = this.getAttribute('data-category-name');
                    
                    deleteCategoryNameElement.textContent = categoryName;
                    deleteCategoryForm.action = `/categories/${categoryId}/delete`;
                    deleteCategoryModal.show();
                });
            });
        });
    </script>
</body>
</html>